/*
 * Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
 */

package cn.knet.sp.ncrm.common.utils;

import java.util.LinkedList;

/******************************************
 * ?* 字符串解析器，包含句子分割、全角转半角等功能
 * 
 * ?
 ******************************************/

public class StringParser {

	private StringBuffer content = null;

	public StringParser(String str) {

		if (str != null)

			this.content = new StringBuffer(str);

	}

	public StringParser() {
		content = new StringBuffer();
	}

	public boolean isEmpty() {

		return content == null || content.length() == 0;

	}

	public void setContent(String string) {

		content.delete(0, content.length());

		content.append(string);

	}

	/****************************************
	 * 
	 * ?
	 * 
	 * 读一个句子
	 * 
	 * Read a sentence from the content
	 * 
	 * @return a substring of the content
	 * 
	 *         ?
	 ****************************************/

	public String readSentence() {

		int tmpindex = Integer.MAX_VALUE, index = Integer.MAX_VALUE;

		String sentence;

		// 找到一句的结尾

		if ((tmpindex = content.indexOf("\r")) > 0)

			index = tmpindex;

		if ((tmpindex = content.indexOf("\n")) > 0)

			index = (tmpindex < index) ? tmpindex : index;

		if ((tmpindex = content.indexOf("。")) > 0)

			index = (tmpindex < index) ? tmpindex : index;

		if ((tmpindex = content.indexOf("!")) > 0)

			index = (tmpindex < index) ? tmpindex : index;

		if ((tmpindex = content.indexOf("?")) > 0)

			index = (tmpindex < index) ? tmpindex : index;

		if (index == Integer.MAX_VALUE) {// 找不到代表句子结尾的标志，可能是一句尚未结束，因此先处理剩余的句子

			sentence = content.toString();

			content.delete(0, content.length());

		}

		else {

			sentence = content.substring(0, index + 1);// [start,end)

			content.delete(0, index + 1);

		}

		return sentence.trim();

	}

	/**********************************************
	 * 
	 * ?
	 * 
	 * 对字符串按空白字符进行分割，将所有子串存储在一个链表中
	 * 
	 * @param str
	 *            任意字符串
	 * 
	 * @return 包含结果的链表
	 * 
	 *         ?
	 **********************************************/

	public LinkedList<String> splitToArray(String str) {

		LinkedList<String> sentence = new LinkedList<String>();

		str = str.replaceAll("[\\s]+", " ");// 正则表达式，将一个或多个制表符替换成空格

		StringBuffer sBuffer = new StringBuffer(str.trim());

		String tmpString;

		int index = 0;

		while (sBuffer.length() > 0) {

			index = sBuffer.indexOf(" ");

			if (index > 0) {

				tmpString = sBuffer.substring(0, index);

				sentence.add(tmpString);

				sBuffer.delete(0, index + 1);

			}

			else if (index == 0) {

				sBuffer.delete(0, 1);

			}

			else {

				sentence.add(sBuffer.toString());

				sBuffer.delete(0, sBuffer.length());

			}

		}

		return sentence;

	}

	/***************************************
	 * 
	 * ?
	 * 
	 * 全角转换成半角
	 * 
	 * @param input
	 *            原始字符串
	 * 
	 * @return 转换后的字符串
	 * 
	 * 
	 ***************************************/

	public static String QtoB(String input) {

		char c[] = input.toCharArray();

		for (int i = 0; i < c.length; i++) {

			if (c[i] == '\u3000') {

				c[i] = ' ';

			}

			else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {

				c[i] = (char) (c[i] - 65248);

			}

		}

		return new String(c);

	}

	public static String BtoQ(String input) {

		char c[] = input.toCharArray();

		for (int i = 0; i < c.length; i++) {

			if (c[i] == ' ') {

				c[i] = '\u3000';

			}

			else if (c[i] < '\177') {

				c[i] = (char) (c[i] + 65248);

			}

		}

		return new String(c);

	}

	public static void main(String[] args)

	{

		String s1 = "杭州马塍路６－１号６０２室";

		System.out.println(QtoB(s1));

		String s2 = "杭州马塍路6-1号602室";
		
//全角转半角
		System.out.println(QtoB(s1).equals(s2));

		System.out.println(s1.equals(s2));
		//半角转全角测试
		System.out.println(BtoQ(s2).equals(s2));

		System.out.println(s1.equals(s2));

	}
}
